package org.mylitespring.test.v2;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.mylitespring.beans.BeanDefinition;
import org.mylitespring.beans.PropertyValue;
import org.mylitespring.beans.factory.config.RuntimeBeanReference;
import org.mylitespring.beans.factory.support.DefaultBeanFactory;
import org.mylitespring.beans.factory.xml.XmlBeanDefinitionReader;
import org.mylitespring.core.io.ClassPathResource;
 

public class BeanDefinitionTestV2 {

	@Test
	public void testGetBeanDefinition() {
		
		DefaultBeanFactory factory=new DefaultBeanFactory();
		XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);
		
		reader.loadBeanDefinitions(new ClassPathResource("petstore-v2.xml"));
		BeanDefinition bd=factory.getBeanDefinition("petStore");
		
		List<PropertyValue> pvs=bd.getPropertyValues();
		Assert.assertTrue(pvs.size()==4);
		{
			PropertyValue pv=this.getPropertyValue("accountDao",pvs);
			Assert.assertNotNull(pv);
			Assert.assertTrue(pv.getValue() instanceof RuntimeBeanReference);
		}
		
		{
			PropertyValue pv=this.getPropertyValue("itemDao",pvs);
			Assert.assertNotNull(pv);
			Assert.assertTrue(pv.getValue() instanceof RuntimeBeanReference);
		}
	}

	private PropertyValue getPropertyValue(String name, List<PropertyValue> pvs) {
		for (PropertyValue propertyValue : pvs) {
			if (propertyValue.getName().equals(name)) {
				return propertyValue;
			}
		}
		return null;
	}
}
